#!/usr/bin/perl
use Time::Local;
use DBI;
use Config::IniFiles;

 sub getdbConn{
  #my $dbname="/var/www/html/RCOOS/website/latest.db";

  #my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "",
  #                   { RaiseError => 1, AutoCommit => 1 });
  #if(!defined $dbh) {die "Cannot connect to database!\n";}

  my $cfg=Config::IniFiles->new( -file => '/var/myscheduler/dbConfig.ini');
  my $db_name="rcoos";
  my $db_user=$cfg->val('rcoos','username');
  my $db_passwd=$cfg->val('rcoos','password');

  my $dbh = DBI->connect ( "dbi:Pg:dbname=$db_name", $db_user, $db_passwd);
  if ( !defined $dbh ) {
		$dbh=0;
  }
  return $dbh; 
 }

 sub timeConvert {
 
 my ($mytime,$timezone)= @_;
 
 my $timeinterval;

 $mytime=substr($mytime,0,10)." ".substr($mytime,11,8);
 if($timezone ne "GMT"){
     my $s1=str2time($mytime);
     if($timezone eq "EDT"){$timeinterval=4;}
     if($timezone eq "EST"){$timeinterval=5;}
     my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($s1-$timeinterval*60*60);
     $year=$year+1900;
     $mon=$mon+1;
     if($mon<10){
       $mon="0".$mon;
     }
     if($mday<10){
      $mday="0".$mday;
     }
     if($hour<10){
      $hour="0".$hour;
     }
     if($min<10){
      $min="0".$min;
     }
     if($sec<10){
      $sec="0".$sec;
     }

    $mytime=$year."-".$mon."-".$mday." ".$hour.":".$min.":".$sec;
   }
   return $mytime;
 }

 sub checkRecent {
 
     my ($mytime)= @_;
 
     my $timeinterval;

     $mytime=substr($mytime,0,10)." ".substr($mytime,11,8);
     #$mytime=$mytime." ".tellmeedt();
     my $s1=str2time($mytime);
 
     my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($s1-40*60);
     $year=$year+1900;
     $mon=$mon+1;
     if($mon<10){
       $mon="0".$mon;
     }
     if($mday<10){
      $mday="0".$mday;
     }
     if($hour<10){
      $hour="0".$hour;
     }
     if($min<10){
      $min="0".$min;
     }
     if($sec<10){
      $sec="0".$sec;
     }

    $mytime=$year."-".$mon."-".$mday." ".$hour.":".$min.":".$sec;
   
   return $mytime;
 }

sub tellmeinterval(){
 my $dsttime;
 my $dststring;
 my($sec1,$min1,$hour1,$mday1,$mon1,$year1,$wday1,$yday1,$isdst1)=localtime(time);
 if($isdst1==0){
    $dsttime=5;
    $dststring="EST";
 }
 if($isdst1==1){
    $dsttime=4;
    $dststring="EDT";
 }
 return $dsttime;
}

sub tellmeedt(){
 my $dsttime;
 my $dststring;
 my($sec1,$min1,$hour1,$mday1,$mon1,$year1,$wday1,$yday1,$isdst1)=localtime(time);
 if($isdst1==0){
    $dsttime=5;
    $dststring="EST";
 }
 if($isdst1==1){
    $dsttime=4;
    $dststring="EDT";
 }
 return $dststring;
}
1;
